﻿using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace MillisecondTimer
{
    public partial class MainForm : Form
    {
        //存储鼠标位置,用于移动无边框的窗体
        private Point mPoint;
        //计时器
        private readonly Stopwatch stopwatch;

        private TimeSpan timeSpan;

        public MainForm()
        {
            InitializeComponent();
            //初始化计时器
            stopwatch = new Stopwatch();
        }
        /// <summary>
        /// 退出按钮单击事件
        /// 退出当前程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonExit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }
        /// <summary>
        /// 开始/停止按钮单击事件
        /// 开始计时和停止计时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonTimer_Click(object sender, EventArgs e)
        {
            if (!timer.Enabled)
            {
                timer.Enabled = true;
                stopwatch.Start();
                timer.Start();
                buttonTimer.Text = "暂停";
            }
            else
            {
                stopwatch.Stop();
                timeSpan = stopwatch.Elapsed;
                timer.Stop();
                buttonTimer.Text = "继续";

            }

        }
        /// <summary>
        /// 归零按钮单击事件
        /// 将秒表计时显示归零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonZero_Click(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Enabled = false;
            stopwatch.Reset();
            labelTimer.Text = "00-00:00:00.000";
            buttonTimer.Text = "开始";
        }

        /// <summary>
        /// 鼠标选中窗体按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MTForm_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }
        /// <summary>
        /// 鼠标拖动窗体事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MTForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Location = new Point(Location.X + e.X - mPoint.X, Location.Y + e.Y - mPoint.Y);
            }
        }
        /// <summary>
        /// 秒表计时器。生成格式化的时间
        /// 日-时:分:秒:毫秒
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            timeSpan = stopwatch.Elapsed;
            string day = "00";
            string hour = "00";
            string minute = "00";
            string second = "00";
            string millisecond = "000";
            int num;
            if (timeSpan.Milliseconds.ToString().Length == 1)
            {
                millisecond = "00" + timeSpan.Milliseconds.ToString();
            }
            else
            {
                num = timeSpan.Milliseconds;
                if (num.ToString().Length == 2)
                {
                    num = timeSpan.Milliseconds;
                    millisecond = "0" + num.ToString();
                }
                else
                {
                    num = timeSpan.Milliseconds;
                    if (num.ToString().Length == 3)
                    {
                        num = timeSpan.Milliseconds;
                        millisecond = num.ToString();
                    }
                }
            }
            num = timeSpan.Seconds;
            if (num.ToString().Length == 1)
            {
                num = timeSpan.Seconds;
                second = "0" + num.ToString();
            }
            else
            {
                num = timeSpan.Seconds;
                if (num.ToString().Length == 2)
                {
                    num = timeSpan.Seconds;
                    second = num.ToString();
                }
            }
            num = timeSpan.Minutes;
            if (num.ToString().Length == 1)
            {
                num = timeSpan.Minutes;
                minute = "0" + num.ToString();
            }
            else
            {
                num = timeSpan.Minutes;
                if (num.ToString().Length == 2)
                {
                    num = timeSpan.Minutes;
                    minute = num.ToString();
                }
            }
            num = timeSpan.Hours;
            if (num.ToString().Length == 1)
            {
                num = timeSpan.Hours;
                hour = "0" + num.ToString();
            }
            else
            {
                num = timeSpan.Hours;
                if (num.ToString().Length == 2)
                {
                    num = timeSpan.Hours;
                    hour = num.ToString();
                }
            }
            num = timeSpan.Days;
            if (num.ToString().Length == 1)
            {
                num = timeSpan.Days;
                day = "0" + num.ToString();
            }
            else
            {
                num = timeSpan.Days;
                if (num.ToString().Length == 2)
                {
                    num = timeSpan.Days;
                    day = num.ToString();
                }
            }
            labelTimer.Text = day + "-" + hour + ":" + minute + ":" + second + "." + millisecond;
        }
    }
}
